//
//  UIButton+Extension.swift
//  FenxiaoMS
//
//  Created by Sky on 2018/2/6.
//  Copyright © 2018年 lainkai. All rights reserved.
//

import Foundation

extension UIButton {
    
    /// 创建 button (使用图片名称)（Swift 7 params）
    ///
    /// - Parameters:
    ///   - title: 标题 title
    ///   - fontSize: 标题字体大小
    ///   - isFontBold: 是否粗体
    ///   - imgNameNormal: 默认状态图片名称
    ///   - imgNameHighlighted: 高亮状态图片名称
    ///   - titleColorNormal: 默认状态字体颜色
    ///   - titleColorHighlighted: 高亮状态字体颜色
    convenience init(title: String, fontSize: CGFloat = 14, isFontBold: Bool = false,
                     imgNameNormal: String = "", imgNameHighlighted: String = "", 
                     titleColorNormal: UIColor = UIColor.black, titleColorHighlighted: UIColor = UIColor.lightGray)
    {
        self.init(title: title, fontSize: fontSize, titleColorNormal: titleColorNormal, titleColorHighlighted: titleColorHighlighted)
        
        if imgNameNormal != "" {
            setImage(UIImage.init(named: imgNameNormal), for: .normal)
        }
        if imgNameHighlighted != "" {
            setImage(UIImage.init(named: imgNameHighlighted), for: .highlighted)
        }
        
        titleLabel?.font = isFontBold ? UIFont.boldSystemFont(ofSize: fontSize) : UIFont.systemFont(ofSize: fontSize)
    }
    
    /// 创建 button (使用图片)（Swift 7 params）
    ///
    /// - Parameters:
    ///   - title: 标题 title
    ///   - fontSize: 标题字体大小
    ///   - isFontBold: 是否粗体
    ///   - imageNormal: 默认状态图片
    ///   - imageHighlighted: 高亮状态图片
    ///   - titleColorNormal: 默认状态字体颜色
    ///   - titleColorHighlighted: 高亮状态字体颜色
    convenience init(title: String, fontSize: CGFloat = 14, isFontBold: Bool = false,
                     imageNormal: UIImage, imageHighlighted: UIImage, 
                     titleColorNormal: UIColor = UIColor.black, titleColorHighlighted: UIColor = UIColor.lightGray)
    {
        self.init(title: title, fontSize: fontSize, titleColorNormal: titleColorNormal, titleColorHighlighted: titleColorHighlighted)
        
        setImage(imageNormal, for: .normal)
        setImage(imageHighlighted, for: .highlighted)
        
        titleLabel?.font = isFontBold ? UIFont.boldSystemFont(ofSize: fontSize) : UIFont.systemFont(ofSize: fontSize)
    }
    
    
    /// 创建 button（Swift 4 params）
    ///
    /// - Parameters:
    ///   - title: 标题 title
    ///   - fontSize: 标题字体大小
    ///   - titleColorNormal: 默认状态字体颜色
    ///   - titleColorHighlighted: 高亮状态字体颜色
    convenience init(title: String, fontSize: CGFloat = 14, 
                     titleColorNormal: UIColor, titleColorHighlighted: UIColor)
    {
        self.init()
        
        setTitle(title, for: .normal)
        setTitleColor(titleColorNormal, for: .normal)
        setTitleColor(titleColorHighlighted, for: .highlighted)
        
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        titleLabel?.numberOfLines = 0
        sizeToFit()
    }
    
    /// 创建 button title/ fontSize/ titlecolorNormal/ titleColorHighlighted/ target/ action（Swift 6 params）
    convenience init(title: String, fontSize: CGFloat = 14, 
                     titleColorNormal: UIColor, titleColorHighlighted: UIColor,
                     target: Any?, action: Selector)
    {
        self.init()
        
        setTitle(title, for: .normal)
        setTitleColor(titleColorNormal, for: .normal)
        setTitleColor(titleColorHighlighted, for: .highlighted)
        
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        addTarget(target, action: action, for: .touchUpInside)
        
        titleLabel?.numberOfLines = 0
        sizeToFit()
    }
    
    /// 创建 button (使用图片)（Swift 4 params）
    ///
    /// - Parameters:
    ///   - title: 标题 title
    ///   - fontSize: 标题字体大小
    ///   - imageNormal: 默认状态图片
    ///   - imageHighlighted: 高亮状态图片
    convenience init(title: String, fontSize: CGFloat,
                     imageNormal: UIImage, imageHighlighted: UIImage)
    {
        self.init()
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        setImage(imageNormal, for: .normal)
        setImage(imageHighlighted, for: .highlighted)

        sizeToFit()
    }
    
    /// 创建 button (使用图片名称)（Swift 2 params）
    ///
    /// - Parameters:
    ///   - imageNameNormal: 默认状态图片
    ///   - imageNameHighlighted: 高亮状态图片
    convenience init(imageNameNormal: String, imageNameHighlighted: String)
    {
        self.init()
        
        setImage(UIImage.init(named: imageNameNormal), for: .normal)
        setImage(UIImage.init(named: imageNameHighlighted), for: .highlighted)
        
        sizeToFit()
    }
    
    
    /// 根据 image 和 backgroundImage 创建 button    
    ///
    /// - Parameters:
    ///   - imageName: button 的 image
    ///   - backgroundImageName: button 的 backgroundImage
    convenience init(_ imageName: String, backgroundImageName: String) 
    {
        self.init()
        
        setImage(UIImage.init(named: imageName), for: .normal)
        let imgNameHL = imageName + "_highlighted"
        setImage(UIImage.init(named: imgNameHL), for: .highlighted)
        
        setBackgroundImage(UIImage.init(named: backgroundImageName), for: .normal)
        let bgImgNameHL = backgroundImageName + "_highlighted"
        setBackgroundImage(UIImage.init(named: bgImgNameHL), for: .highlighted)
    }
    
    
    
    
    
    
}



extension UIButton {
    
    func gx_setTitle(title: String) {
        setTitle(title, for: .normal)
        setTitle(title, for: .highlighted)
        
        sizeToFit()
    }
    
    
    
    
    
    
    
    
    
}















